greedy

Please click on ads to support us..

Python Code:

def check(n,A):
    A=sorted(A)
    M=[x%2 for x in A]
    ci=0
    cp=0
    c=0
    imp=0
    par=0
    for i in range(n):
        if M[i]:
            c=c+1
            if ci==0:
                imp=A[i]
                ci=1
        else:
            if cp==0:
                par=A[i]
                cp=1
    if c==0 or c==n:
         print("YES")
    else:
        if imp<par:
            print("YES")
        else:
            print("NO")
t=int(input())
for _ in range(t):
    n=int(input())
    A=[int(x) for x in input().split()]
    check(n,A)

C++ Code:

/* header file */
#include<bits/stdc++.h>
#include<stack>
#include <queue>
#include <algorithm>
#include<iostream>
#include<string>
/* for faster cin cout */
#define fast() ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
/* sort function */
#define strsort() sort(s.begin(),s.end())
#define arrsort() int n1=sizeof(a)/sizeof(a[0]);sort(a,a+n1)
/* legth function */
#define strlen s.size()
#define arrlen int n1=sizeof(a)/sizeof(a[0])
#define intlen int n2=trunc(log10(n))+1
/* Conver function */
#define csti int csti=stoi(s)
/* Others Sortcut */
#define PI 3.1415926535897932384626433832795
#define nn "\n"
#define yes "YES"
#define no "NO"
#define case "Case "
#define elif else if
#define ll long long
/* testcase */
#define test() int t;cin>>t;while(t--)
#define testt() int t;cin>>t;for(int i=1; i<=t; i++)
using namespace std;
const long long MOD = 1e9 + 7;
/*  let's started  */
void solved()
{
    test()
    {
        int n;
        cin>>n;
        ll a[n];
        for(int i=0; i<n; i++)
            cin>>a[i];

        arrsort();
        int c=0,c1=0;
        for(int i=0; i<n; i++)
        {
            if(a[0]%2==1)
            {
                c=1;
                break;
            }
            else
            {
                if(a[i]%2==1)
                {
                    c1++;
                }
            }
        }
        if(c1==0||c==1) cout<<yes<<nn;
        else cout<<no<<nn;
    }
}

int main()
{
    fast();
    solved();
    return 0;
}


Comments

Submit
0 Comments
More Questions

43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos